package com.xiangyueEducation.uploaderCloud.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xiangyueEducation.uploaderCloud.pojo.entity.Chat;
import com.xiangyueEducation.uploaderCloud.service.entity.ChatService;
import com.xiangyueEducation.uploaderCloud.utils.JwtHelper;
import com.xiangyueEducation.uploaderCloud.utils.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.lang.Nullable;
import org.springframework.web.bind.annotation.*;

@RestController
@Slf4j
@CrossOrigin
@RequestMapping("chat")
public class ChatController {
    @Autowired
    JwtHelper jwtHelper;
    @Autowired
    ChatService chatService;


    @GetMapping("history/{receiverId}/{currentPage}")
    public Result<?> getHistory(@RequestHeader String token,
                                @PathVariable Long receiverId,
                                @PathVariable Integer currentPage,
                                @RequestParam @Nullable Integer pageSize){

        Long userId = jwtHelper.getUserId(token);
        if (pageSize == null) pageSize=10;
        Page<Chat> page = new Page<>(currentPage,pageSize);
        return chatService.getHistory(userId,receiverId,page);
    }
}
